thumbstick2 2.2.0
Loading...
Searching...
No Matches
thumbstick2.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2020 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
27
28#ifndef THUMBSTICK2_H
29#define THUMBSTICK2_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_spi_master.h"
52#include "spi_specifics.h"
53#include "drv_pwm.h"
54
60
65
70
75#define THUMBSTICK2_ADC_START_BIT 0x01
76#define THUMBSTICK2_ADC_SINGLE_ENDED 0x80
77#define THUMBSTICK2_ADC_CHANNEL_X 0x00
78#define THUMBSTICK2_ADC_CHANNEL_Y 0x40
79#define THUMBSTICK2_ADC_DATA_RESOLUTION 0x0FFF
80#define THUMBSTICK2_ADC_DATA_RESOLUTION_HALF ( ( float ) THUMBSTICK2_ADC_DATA_RESOLUTION / 2 )
81
86#define THUMBSTICK2_RADIANS_TO_DEGREES 57.295779513
87#define THUMBSTICK2_ANGLE_ZERO_OFFSET 180.0
88#define THUMBSTICK2_PI_RAD_OFFSET 1.0
89#define THUMBSTICK2_NEUTRAL_POSITION_OFFSET ( ( float ) THUMBSTICK2_ADC_DATA_RESOLUTION / 8 )
90#define THUMBSTICK2_FULL_ANGLE 360.0f
91#define THUMBSTICK2_ZERO_ANGLE 0.0f
92#define THUMBSTICK2_ANGLE_POSITION_STEP 45.0f
93
98#define THUMBSTICK2_POSITION_NEUTRAL 0
99#define THUMBSTICK2_POSITION_UP 1
100#define THUMBSTICK2_POSITION_UPPER_LEFT 2
101#define THUMBSTICK2_POSITION_LEFT 3
102#define THUMBSTICK2_POSITION_LOWER_LEFT 4
103#define THUMBSTICK2_POSITION_DOWN 5
104#define THUMBSTICK2_POSITION_LOWER_RIGHT 6
105#define THUMBSTICK2_POSITION_RIGHT 7
106#define THUMBSTICK2_POSITION_UPPER_RIGHT 8
107
112#define THUMBSTICK2_PWM_DEF_FREQ 20000
113#define THUMBSTICK2_PWM_MIN_DUTY 0.0
114#define THUMBSTICK2_PWM_MAX_DUTY 1.0
115
124#define THUMBSTICK2_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
125#define THUMBSTICK2_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
126 // thumbstick2_set
128
133
138
143#define THUMBSTICK2_MAP_MIKROBUS( cfg, mikrobus ) \
144 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
145 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
146 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
147 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
148 cfg.vb = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
149 cfg.sw = MIKROBUS( mikrobus, MIKROBUS_INT )
150 // thumbstick2_map // thumbstick2
153
158typedef struct
159{
160 // Output pins
161 digital_out_t vb;
162
163 // Input pins
164 digital_in_t sw;
165
166 // Modules
167 pwm_t pwm;
168 spi_master_t spi;
169
170 pin_name_t chip_select;
171 uint32_t pwm_freq;
172
174
179typedef struct
180{
181 // Communication gpio pins
182 pin_name_t miso;
183 pin_name_t mosi;
184 pin_name_t sck;
185 pin_name_t cs;
186 pin_name_t vb;
187
188 // Additional gpio pins
189 pin_name_t sw;
190
191 // static variable
192 uint32_t spi_speed;
193 spi_master_mode_t spi_mode;
194 spi_master_chip_select_polarity_t cs_polarity;
195
196 uint32_t dev_pwm_freq;
197
199
210
216
227
242
255err_t thumbstick2_read_raw_adc ( thumbstick2_t *ctx, uint16_t *raw_x, uint16_t *raw_y );
256
265float thumbstick2_get_angle ( uint16_t raw_x, uint16_t raw_y );
266
275uint8_t thumbstick2_get_position ( uint16_t raw_x, uint16_t raw_y );
276
287err_t thumbstick2_set_duty_cycle ( thumbstick2_t *ctx, float duty_cycle );
288
299
310
320
321#ifdef __cplusplus
322}
323#endif
324#endif // THUMBSTICK2_H
325 // thumbstick2
327
328// ------------------------------------------------------------------------ END
uint8_t thumbstick2_get_sw_pin(thumbstick2_t *ctx)
Thumbstick 2 get SW pin function.
err_t thumbstick2_pwm_start(thumbstick2_t *ctx)
Thumbstick 2 start PWM function.
err_t thumbstick2_pwm_stop(thumbstick2_t *ctx)
Thumbstick 2 stop PWM function.
float thumbstick2_get_angle(uint16_t raw_x, uint16_t raw_y)
Thumbstick 2 get angle function.
err_t thumbstick2_init(thumbstick2_t *ctx, thumbstick2_cfg_t *cfg)
Thumbstick 2 initialization function.
uint8_t thumbstick2_get_position(uint16_t raw_x, uint16_t raw_y)
Thumbstick 2 get position function.
err_t thumbstick2_set_duty_cycle(thumbstick2_t *ctx, float duty_cycle)
Thumbstick 2 set duty cycle function.
void thumbstick2_cfg_setup(thumbstick2_cfg_t *cfg)
Thumbstick 2 configuration object setup function.
err_t thumbstick2_read_raw_adc(thumbstick2_t *ctx, uint16_t *raw_x, uint16_t *raw_y)
Thumbstick 2 read raw adc function.
This file contains SPI specific macros, functions, etc.
Thumbstick 2 Click configuration object.
Definition thumbstick2.h:180
pin_name_t vb
Definition thumbstick2.h:186
spi_master_chip_select_polarity_t cs_polarity
Definition thumbstick2.h:194
pin_name_t sck
Definition thumbstick2.h:184
spi_master_mode_t spi_mode
Definition thumbstick2.h:193
pin_name_t mosi
Definition thumbstick2.h:183
uint32_t spi_speed
Definition thumbstick2.h:192
uint32_t dev_pwm_freq
Definition thumbstick2.h:196
pin_name_t sw
Definition thumbstick2.h:189
pin_name_t miso
Definition thumbstick2.h:182
pin_name_t cs
Definition thumbstick2.h:185
Thumbstick 2 Click context object.
Definition thumbstick2.h:159
spi_master_t spi
Definition thumbstick2.h:168
uint32_t pwm_freq
Definition thumbstick2.h:171
digital_in_t sw
Definition thumbstick2.h:164
digital_out_t vb
Definition thumbstick2.h:161
pin_name_t chip_select
Definition thumbstick2.h:170
pwm_t pwm
Definition thumbstick2.h:167
thumbstick2_return_value_t
Thumbstick 2 Click return value data.
Definition thumbstick2.h:205
@ THUMBSTICK2_OK
Definition thumbstick2.h:206
@ THUMBSTICK2_ERROR
Definition thumbstick2.h:207